package com.zb.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

public class CommonUtils {

    public static boolean isEmpty(String s) {
        return null == s || StringUtils.isEmpty(s.trim());
    }

    public static boolean isEmpty(Collection<?> s) {
        return null == s || s.isEmpty();
    }

    public static boolean isNotEmpty(Collection<?> s) {
        return !isEmpty(s);
    }

    public static boolean isNotEmpty(String redirect) {
        return !isEmpty(redirect);
    }

    public static Date stringToDate(String dateString) {
        if (CommonUtils.isEmpty(dateString)) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            return null;
        }

    }

    public static String nowDateToString(String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
            return simpleDateFormat.format(new Date());
        } catch (Exception e) {
            return null;
        }

    }

    public static String DateToString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return simpleDateFormat.format(date);
        } catch (Exception e) {
            return null;
        }

    }

    public static String joinKeyValue(Map<String, Object> map, String prefix, String suffix, String separator,
            boolean ignoreEmptyValue, String... ignoreKeys) {
        List<String> list = new ArrayList<String>();
        if (map != null) {
            for (Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                String value = ConvertUtils.convert(entry.getValue());
                if (StringUtils.isNotEmpty(key) && !ArrayUtils.contains(ignoreKeys, key)
                        && (!ignoreEmptyValue || StringUtils.isNotEmpty(value))) {
                    list.add(key + "=" + (value != null ? value : "") + "");
                }
            }
        }
        String s = (prefix != null ? prefix : "") + StringUtils.join(list, separator) + (suffix != null ? suffix : "");
        return s;
    }

    public static String joinKeyValue(Map<String, Object> map, String prefix, String suffix, String valuePrefix,
            String valueSuffix, String separator, boolean ignoreEmptyValue, String... ignoreKeys) {
        List<String> list = new ArrayList<String>();
        if (map != null) {
            for (Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                String value = ConvertUtils.convert(entry.getValue());
                if (StringUtils.isNotEmpty(key) && !ArrayUtils.contains(ignoreKeys, key)
                        && (!ignoreEmptyValue || StringUtils.isNotEmpty(value))) {
                    list.add(key + "=" + valuePrefix + (value != null ? value : "") + valueSuffix);
                }
            }
        }
        String s = (prefix != null ? prefix : "") + StringUtils.join(list, separator) + (suffix != null ? suffix : "");
        return s;
    }

    public static String joinValue(Map<String, Object> map, String prefix, String suffix, String separator,
            boolean ignoreEmptyValue, String... ignoreKeys) {
        List<String> list = new ArrayList<String>();
        if (map != null) {
            for (Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                String value = ConvertUtils.convert(entry.getValue());
                if (StringUtils.isNotEmpty(key) && !ArrayUtils.contains(ignoreKeys, key)
                        && (!ignoreEmptyValue || StringUtils.isNotEmpty(value))) {
                    list.add(value != null ? value : "");
                }
            }
        }
        return (prefix != null ? prefix : "") + StringUtils.join(list, separator) + (suffix != null ? suffix : "");
    }

    /**
     * 分转元
     * 
     * @param money
     * @return
     */
    public static String money(Long money) {
        if (money == null) {
            return "0.00";
        }
        return new BigDecimal(money).divide(new BigDecimal(100)).setScale(2, RoundingMode.DOWN).toString();
    }

    /**
     * g转kg
     * 
     * @param money
     * @return
     */
    public static String weight(Long weight) {
        if (weight == null) {
            return "0.000";
        }
        return new BigDecimal(weight).divide(new BigDecimal(1000)).setScale(3, RoundingMode.DOWN).toString();
    }

    /**
     * kg转g
     * 
     * @param money
     * @return
     */
    public static Long weight(String weight) {
        if (CommonUtils.isEmpty(weight)) {
            return null;
        }
        if (!weight.matches("^[0-9]+(\\.[0-9]{1,3})?$")) {
            return null;
        }
        return new BigDecimal(weight).multiply(new BigDecimal(1000)).longValue();
    }

    /**
     * 分转元
     * 
     * @param money
     * @return
     */
    public static String fen2yuan(String money) {
        if (money == null) {
            return "0.00";
        }
        return new BigDecimal(money).divide(new BigDecimal(100)).setScale(2, RoundingMode.DOWN).toString();
    }

    /**
     * 元转分
     * 
     * @param money
     * @return
     */
    public static Long money(String money) {
        if (CommonUtils.isEmpty(money)) {
            return null;
        }
        if (!money.matches("^[0-9]+(\\.[0-9]{1,2})?$")) {
            return null;
        }
        return new BigDecimal(money).multiply(new BigDecimal(100)).longValue();
    }

}
